var util = require('../../../utils/util.js');
Page({
  data: {
    tips: '等待中...',
    errCode: 0,
    TabCur: 0,
    scrollLeft: 0,
    order_list: [],
    order_length: 0,
    deliver_list: [],
    deliver_length: 0,
    order_limit: 5,
    order_skip: 0,
    deliver_limit: 5,
    deliver_skip: 0,
    tabList: ['扫码发货', '订单发货', '发货记录'],
    numList: [{
      name: '扫卡券码',
      bindtap: 'scanCode'
    }, {
      name: '扫快递码',
      bindtap: 'scanCode'
    }, {
      name: '完成发货',
      bindtap: ''
    }],
    num: 0,
  },
  onLoad: function (options) {

  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60,
      express_number: ''
    });
    if (e.currentTarget.dataset.id == 1) {
      this.tabGetOrder();
    } else if (e.currentTarget.dataset.id == 2) {
      if (util.get('deliver_list', 'outTime') == 'outTime') {
        this.setData({
          limit: 5,
          skip: 0,
          deliver_list: []
        });
        this.getOrder('deliver', this.data.limit, this.data.skip, 4, 2);
      } else {
        console.log('【发货记录】页面数据调用缓存。');
        wx.hideLoading();
        this.setData({
          deliver_list: wx.getStorageSync('deliver_list'),
          deliver_length: wx.getStorageSync('deliver_length')
        })
      }
    }
  },
  // 获取待发货列表
  tabGetOrder() {
    if (util.get('order_list', 'outTime') == 'outTime') {
      this.setData({
        limit: 5,
        skip: 0,
        order_list: []
      });
      this.getOrder('order', this.data.limit, this.data.skip, 3, 1);
    } else {
      console.log('【订单发货】页面数据调用缓存。');
      wx.hideLoading();
      this.setData({
        order_list: wx.getStorageSync('order_list'),
        order_length: wx.getStorageSync('order_length')
      })
    }
  },
  scanCode(e) {
    var that = this;
    wx.showLoading({
      title: '系统正在处理',
    })
    wx.scanCode({
      onlyFromCamera: false,
      scanType: ['qrCode', 'barCode'],
      success: function (res) {
        wx.hideLoading();
        if (that.data.TabCur == 0) {
          // 赋值
          that.setData({
            [e.currentTarget.dataset.source]: res.result,
            num: that.data.num + 1
          });
          if (that.data.num == 2) {
            that.updateOrders('scan');
          }
        } else if (that.data.TabCur == 1) {
          that.setData({
            express_number: res.result
          });
        }
      },
      fail: function (err) {
        // console.log(err)
        wx.showToast({
          title: '扫码失败',
          icon: 'error',
          duration: 2000
        })
      },
      complete: function (res) {
        // console.log(res)
      }
    })
  },
  // 获取订单
  getOrder(name, limit, skip, card_status, order_status) {
    var that = this;
    let times = 60;
    wx.showLoading({
      title: '系统正在处理',
    })
    wx.cloud.callFunction({
      name: "OrderFunctions",
      data: {
        type: 'getOrder',
        limit: limit,
        skip: skip,
        card_status: card_status,
        order_status: order_status,
      },
      success: res => {
        wx.hideLoading();
        // console.log(res);
        // console.log([name] + '_limit');
        let limit = [name] + '_limit';
        let length = [name] + '_length';
        let list = [name] + '_list';
        that.setData({
          [limit]: limit,
          [length]: res.result.length,
          [list]: res.result.list
        });
        if (name == 'order') {
          if (that.data.order_length == that.data.order_list.length) {
            util.put('order_list', that.data.order_list, times);
            util.put('order_length', that.data.order_length, times);
          }
        } else if (name == 'deliver') {
          util.put('deliver_list', that.data.deliver_list, times);
          util.put('deliver_length', that.data.deliver_length, times);
        }
      }
    })
  },
  // 订单卡片上发货按钮
  deliverGoods(e) {
    this.setData({
      goods_id: e.currentTarget.dataset.info,
      card_id: e.currentTarget.dataset.card,
      modalName: 'DialogModal'
    });
  },
  // 订单发货模态框确认按钮
  expNumberSubmit(e) {
    if (this.data.express_number == '' || this.data.express_number == null) {
      wx.showToast({
        title: '快递单号错误',
        icon: 'error'
      }, 2000)
    } else {
      this.setData({
        modalName: null
      });
      this.updateOrders('deliver');
    }
  },
  // 进行发货，更新订单
  updateOrders(source) {
    var that = this;
    wx.showLoading({
      title: '系统正在处理',
    })
    wx.cloud.callFunction({
      name: 'OrderFunctions',
      data: {
        type: 'updateOrder',
        card_number: that.data.card_id != null ? '' : that.data.card_info.split(',')[0],
        card_password: that.data.card_id != null ? '' : that.data.card_info.split(',')[1],
        express_number: that.data.express_number,
        card_id: that.data.card_id != null ? that.data.card_id : '',
        source: source
      },
      success: res => {
        wx.hideLoading();
        // console.log(res);
        if (res.result == 1) {

          if (that.data.TabCur == 0) {
            that.setData({
              tips: '完成发货',
              errCode: 0
            });
          } else if (that.data.TabCur == 1) {
            wx.showToast({
              title: '完成发货',
              icon: 'success'
            }, 2000)
            wx.removeStorageSync('order_list');
            wx.removeStorageSync('order_length');
            // 延时请求数据
            setTimeout(function () {
              that.tabGetOrder();
            }, 2000)
          }
        } else {
          that.setData({
            tips: res.result,
            errCode: 1
          })
        }
      }
    })
  },
  // 继续发货
  setNum() {
    var that = this;
    that.setData({
      num: 0,
      errCode: 0,
      tips: '等待中...'
    })
  },
  // 复制信息到粘贴板
  copyData(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.info,
      success() {
        wx.showToast({
          title: '复制成功',
          icon: 'success',
          duration: 2000
        })
      }
    })
  },
  // 拨打电话
  callPhone(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.number,
    })
  },
  // 获取输入的快递单号
  getExpNumber(e) {
    this.setData({
      express_number: e.detail.value
    })
  },
  // 隐藏模态框
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  // 上拉触底,获取发货记录
  onReachBottom() {
    console.log('上拉触底');
    if (this.data.deliver_list.length < this.data.lssue_length) {
      this.getDeliverGoods(this.data.limit + 5, this.data.skip + this.data.limit);
    } else {
      wx.showToast({
        title: '数据加载完毕',
        icon: 'success'
      })
    }
  }
})